package com.coderman.infosys.auth.adapter.controller.system;

import com.coderman.infosys.auth.adapter.controller.BaseController;
import com.coderman.infosys.auth.adapter.convert.MenuConverter;
import com.coderman.infosys.auth.domain.bo.system.SystemBO;
import com.coderman.infosys.auth.domain.enums.errors.MenuErrorEnum;
import com.coderman.infosys.auth.domain.factory.SystemFactory;
import com.coderman.infosys.auth.domain.gataway.SystemModuleQueryGataWay;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import com.coderman.infosys.plat.auth.common.page.PageBO;
import com.coderman.infosys.plat.auth.common.vo.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coderman.infosys.auth.adapter.vo.system.MenuVO;
import com.coderman.infosys.auth.adapter.vo.system.request.UpdateMenuRequestVO;
import com.coderman.infosys.auth.adapter.vo.system.request.CreateMenuRequestVO;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.coderman.infosys.auth.domain.bo.system.MenuBO;
import com.coderman.infosys.auth.domain.repository.SystemModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.*;
import java.util.List;


/**
* @Description:控制层
* @Author:
* @CreateTime:2022-01-04 15:27:39
* @version v1.0
*/
@RestController
public class MenuController extends BaseController {
	
	protected Logger logger = LoggerFactory.getLogger(MenuController.class);

	@Autowired
	private SystemModuleRepository systemModuleRepository;

	@Autowired
	private SystemModuleQueryGataWay systemModuleQueryGataWay;

	@Autowired
	private SystemFactory systemFactory;


	/**
	 *
	 * @Description 修改按钮信息
	 * @param updateMenuRequestVO
	 * @return Boolean
	 */
	@RequestMapping(value = "/menu/updatemenu")
	public ResultDataDto<Boolean> updateMenu(@RequestBody UpdateMenuRequestVO updateMenuRequestVO){
        MenuBO menuBO = MenuConverter.INSTANCE.vo2bo(updateMenuRequestVO);
		//构建一个聚合根对象,聚合根的构建过程在systemFactory中,在主流程中屏蔽构建过程
		SystemBO systemBO = systemFactory.initSystemByMenu(menuBO);
		systemModuleRepository.updateSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}

	/**
	 *
	 * @Description 创建按钮信息
	 * @param createMenuRequestVO
	 * @return MenuVO
	 */
	@RequestMapping(value = "/menu/createmenu")
	public ResultDataDto<Boolean> createMenu(@RequestBody CreateMenuRequestVO createMenuRequestVO){
        MenuBO menuBO = MenuConverter.INSTANCE.vo2bo(createMenuRequestVO);
		SystemBO systemBO = systemFactory.initSystemByMenu(menuBO);
		systemModuleRepository.registSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}

	/**
	 *
	 * @Description 获取按钮详情信息
	 * @param menuCode
	 * @return MenuVO
	 */
	@RequestMapping(value = "/menu/getbycode")
	public ResultDataDto<MenuVO> getByCode(@RequestParam(value = "menuCode", required = true) String menuCode){
		MenuBO menuBO = systemModuleRepository.getMenuBO(menuCode);
		if(menuBO == null){
			return ResultDataDto.fail(MenuErrorEnum.MENU_NOT_FUND);
		}
		return ResultDataDto.success(MenuConverter.INSTANCE.bo2VO(menuBO));
	}

	/**
	 *
	 * @Description 获取按钮列表信息
	 * @param pageVO
	 * @return PageVO<MenuVO>
	 */
	@RequestMapping(value = "/menu/pagelist")
	public ResultDataDto<PageVO> getPageList(PageVO pageVO){
		PageBO pageBO = pageVO.convertToBO();
		List<MenuBO> menuBOList = systemModuleQueryGataWay.queryMenuPage(pageBO);
		return ResultDataDto.success(pageVO.initData(MenuConverter.INSTANCE.boList2voList(menuBOList),pageBO.getTotalRows()));
	}
}
